﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PMS.SANY.Entity
{
    public class GDbContextFactory : IDesignTimeDbContextFactory<GDbContext>
    {
        public GDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<GDbContext>();
            var serverVersion = new MySqlServerVersion(new Version(8, 0, 27));
            //optionsBuilder.UseMySql("server=10.76.92.202;user id=root;password=ZEQP@2022;persistsecurityinfo=True;database=pms_xcmg;SslMode=none;CharSet=utf8mb4;", serverVersion);            
            //optionsBuilder.UseMySql("server=192.168.98.198;user id=root;password=zeqp@2021;persistsecurityinfo=True;database=ZeqpPms;SslMode=none;CharSet=utf8mb4;", serverVersion);
            //optionsBuilder.UseMySql("server=139.9.69.110;user id=root;password=ABCabc123;persistsecurityinfo=True;database=ctr_pms_sany;SslMode=none;CharSet=utf8mb4;", serverVersion); 
            optionsBuilder.UseMySql("server=10.28.0.51;user id=root;password=DELL-2020;persistsecurityinfo=True;database=ctr_pms_sany;SslMode=none;CharSet=utf8mb4;", serverVersion); 
            //optionsBuilder.UseSqlServer("Data Source=114.115.162.100;Initial Catalog=ZEQP;User Id=wms;Password=AmwPs1129;APP=ZEQPAdmin;Pooling=true;");
            //optionsBuilder.UseMySql("server=localhost;user id=root;password=root;persistsecurityinfo=True;database=ctr_pms_sany;SslMode=none;CharSet=utf8mb4;", serverVersion);
            optionsBuilder.EnableSensitiveDataLogging();
            return new GDbContext(optionsBuilder.Options);
        }

        public GDbContext DbContext()
        {
            return CreateDbContext(null);
        }
    }
    public class GDbContext : DbContext
    {
        public GDbContext(DbContextOptions<GDbContext> options)
            : base(options)
        {
        }

        /// <summary>
        /// 模型创建
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);

            modelBuilder.HasDbFunction(typeof(GDbContext).GetMethod(nameof(DateDiffDay), new[] { typeof(string), typeof(string) }))
            .HasTranslation(args =>
                new SqlFunctionExpression("DATEDIFF",
                new[] { args.Skip(1).First(), args.First() },
                nullable: true,
                argumentsPropagateNullability: new[] { true, true },
                typeof(int),
                null)
            );

            modelBuilder.HasDbFunction(typeof(GDbContext).GetMethod(nameof(DateDiffDay), new[] { typeof(string), typeof(DateTime) }))
                .HasTranslation(args =>
                    new SqlFunctionExpression("DATEDIFF",
                    new[] { args.Skip(1).First(), args.First() },
                    nullable: true,
                    argumentsPropagateNullability: new[] { true, true },
                    typeof(int),
                    null)
                );
        }

        #region Sys

        /// <summary>
        /// 组织架构
        /// </summary>
        public DbSet<Sys_Org> Sys_Org { get; set; }
        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<Sys_User> Sys_User { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<Sys_Role> Sys_Role { get; set; }

        /// <summary>
        /// 用户角色
        /// </summary>
        public DbSet<Sys_UserRole> Sys_UserRole { get; set; }

        /// <summary>
        /// 菜单
        /// </summary>
        public DbSet<Sys_Menu> Sys_Menu { get; set; }

        /// <summary>
        /// 菜单权限
        /// </summary>
        public DbSet<Sys_RoleMenu> Sys_RoleMenu { get; set; }

        /// <summary>
        /// 操作定义
        /// </summary>
        public DbSet<Sys_Action> Sys_Action { get; set; }

        /// <summary>
        /// 操作权限
        /// </summary>
        public DbSet<Sys_RoleAction> Sys_RoleAction { get; set; }

        /// <summary>
        /// 人员
        /// </summary>
        public DbSet<Sys_Personnel> Sys_Personnel { get; set; }
        #endregion

        #region CF
        public DbSet<CF_CodeRule> CF_CodeRule { get; set; }
        public DbSet<CF_CodeSerial> CF_CodeSerial { get; set; }
        public DbSet<CF_CodeType> CF_CodeType { get; set; }
        public DbSet<CF_Config> CF_Config { get; set; }
        public DbSet<CF_District> CF_District { get; set; }
        public DbSet<CF_Enum> CF_Enum { get; set; }
        public DbSet<CF_EnumItem> CF_EnumItem { get; set; }
        #endregion

        #region Define
        public DbSet<MaterielSpec> MaterielSpec { set; get; }
        public DbSet<MaterialBom> MaterialBom { set; get; }
        public DbSet<MaterialBomTree> MaterialBomTree { set; get; }
        public DbSet<MaterielInfor> MaterielInfor { set; get; }
        #endregion

        #region Exec
        public DbSet<Plan> Plan { set; get; }
        public DbSet<OprSequence> OprSequence { set; get; }
        public DbSet<Component> Component { set; get; }
        public DbSet<StationExecuted> StationExecuted { set; get; }
        public DbSet<StationExecutedKey> StationExecutedKey { set; get; }
        #endregion

        #region Helper
        public int DateDiffDay(string start, string end) => throw new NotSupportedException();
        public int DateDiffDay(string start, DateTime end) => throw new NotSupportedException();
        #endregion
    }
}
